const log4 = require("log4js")
const path = require("path")

function getCommonAppeder(path){
    return {
            type: "dateFile",
            filename: require("path").resolve(__dirname, `../logs/${path}/logger.log`),
            keepFileExt: true, // 配置日志文件是否保留扩展名
            maxLogSize: 1024 * 150, // 配置日志文件最大字节数
            numBackups: 3,
        }
}

// 配置日志输出格式及分类
log4.configure({
    appenders: {
        sql: getCommonAppeder("sql"),
        default: {
            type: "stdout"
        },
        api: getCommonAppeder("api"),
    },
    categories: {
        sql: {
            appenders: ["sql"],  //使用出口sql的配置
            level: "all",   //配置日志输出级别为all
        },
        default: {
            appenders: ["default"],
            level: "all",
        },
        api: {
            appenders: ["api"],
            level: "all",
        }
    }
})

// 监听进程退出事件,在进程退出前关闭日志记录器
process.on("exit", () => {
    log4.shutdown()
})


exports.loggerSql = log4.getLogger("sql")
exports.loggerDefault = log4.getLogger()
exports.loggerApi = log4.getLogger("api")